﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace inf_1007_devoir3
{
    public partial class frmPaiementLocation : Form
    {
        private frmListeLocations frmListe;
        private Location location;
        private Location oldLocation;
        private bool termine;

        public frmPaiementLocation(frmListeLocations frmListe, Location loc)
        {
            this.frmListe = frmListe;
            location = loc;
            termine = false;
            InitializeComponent();
        }

        public frmPaiementLocation(frmListeLocations frmListe, Location loc, Location oldLoc)
        {
            this.frmListe = frmListe;
            location = loc;
            oldLocation = oldLoc;
            termine = false;
            InitializeComponent();
        }

        private void frmPaiementLocation_Load(object sender, EventArgs e)
        {
            lblNbJours.Text = String.Format("{0:0.00}", location.nombreDeJours()) + "$";
            lblMontantJour.Text = String.Format("{0:0.00}", location.montantLocationParJour()) + "$";
            lblMontantLocation.Text = String.Format("{0:0.00}", location.calculSousTotalLocation()) + "$";
            lblTPS.Text = String.Format("{0:0.00}", location.calculMontantTPS()) + "$";
            lblTVQ.Text = String.Format("{0:0.00}", location.calculMontantTVQ()) + "$";
            lblDepotGarantie.Text = String.Format("{0:0.00}", location.depotGarantie()) + "$";
            lblMontantDejaVerse.Text = String.Format("{0:0.00}", location.getMontantPaye() * -1) + "$";
            lblMontantAPayer.Text = String.Format("{0:0.00}", location.calculMontantTotal()) + "$";
        }

        private void btnTerminer_Click(object sender, EventArgs e)
        {
            if (valider())
            {
                termine = true;
                CtrlLocation.getInstance().payer(location, oldLocation, Convert.ToDouble(txtMontant.Text.Trim()), chkContrat.Checked);
                frmListe.remplirListe();
                this.Close();
            }
        }

        private bool valider()
        {
            bool valide = true;
            error.Clear();

            if (!chkContrat.Checked)
            {
                error.SetError(chkContrat, "Le client doit signer le contrat.");
                valide = false;
            }

            double montantPaye = -1;

            try
            {
                montantPaye = Convert.ToDouble(txtMontant.Text.Trim());
            }
            catch (Exception e)
            {
                error.SetError(txtMontant, "Veuillez entrer un montant au format numérique.");
                valide = false;
            }

            if (montantPaye != -1 && montantPaye != location.calculMontantTotal())
            {
                error.SetError(txtMontant, "Le client doit payer la totalité du montant.");
                valide = false;
            }

            return valide;
        }

        private void btnAnnuler_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPaiementLocation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (oldLocation != null && !termine)
            {
                oldLocation.copier(location);
            }
        }
    }
}
